package com.xuecheng.content.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.xuecheng.commons.constants.CourseConstants;
import com.xuecheng.commons.enums.ErrorCode;
import com.xuecheng.commons.model.dto.BindTeachplanMediaDto;
import com.xuecheng.commons.model.vo.MediaVo;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.utils.AuthInfoHolder;
import com.xuecheng.content.domain.po.CourseBase;
import com.xuecheng.content.domain.po.Teachplan;
import com.xuecheng.content.domain.po.TeachplanMedia;
import com.xuecheng.content.mapper.TeachplanMediaMapper;
import com.xuecheng.content.service.CourseBaseService;
import com.xuecheng.content.service.TeachplanMediaService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.content.service.TeachplanService;
import com.xuecheng.media.api.MediaClient;
import com.xuecheng.web.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author itheima
 * @since 2024-05-06
 */
@Service
class TbTeachplanMediaServiceImpl extends ServiceImpl<TeachplanMediaMapper, TeachplanMedia> implements TeachplanMediaService {

    @Autowired
    private TeachplanService teachplanService;

    @Autowired
    private CourseBaseService courseBaseService;

    @Autowired
    private MediaClient mediaClient;

    //计划绑定媒资
    @Override
    public ResponseResult association(BindTeachplanMediaDto dto) {
        //1.参数校验
        if(dto.getMediaId() == null || ObjectUtil.isEmpty(dto.getTeachplanId())){
            throw new BusinessException(ErrorCode.PARAMSERROR);
        }
        //2.判断课程状态
        //2-1先查询课程计划
        Teachplan teachplan = teachplanService.getById(dto.getTeachplanId());
        //2-2查询课程信息
        CourseBase courseBase = courseBaseService.getById(teachplan.getCourseId());
        //2-3校验状态
        if(StrUtil.equals(courseBase.getAuditStatus(), CourseConstants.AUDIT_COMMIT_STATUS)
        || StrUtil.equals(courseBase.getAuditStatus(),CourseConstants.AUDIT_PASTED_STATUS)
        || StrUtil.equals(courseBase.getAuditStatus(),CourseConstants.AUDIT_PUBLISHED_STATUS)){
            throw new BusinessException(ErrorCode.MEDIABINDERROR);
        }
        //3.feign远程调用
        ResponseResult<MediaVo> responseResult = mediaClient.findById(dto.getMediaId());
        //3-1 判断是否调用成功
        if(responseResult.getCode() != 0){
            throw new BusinessException(ErrorCode.DATAERROR);
        }
        //3-2取出媒资对象
        MediaVo mediaVo = responseResult.getData();
        //4.封装媒资绑定实体
        TeachplanMedia teachplanMedia = new TeachplanMedia();
        teachplanMedia.setMediaId(mediaVo.getId());
        teachplanMedia.setTeachplanId(teachplan.getId());
        teachplanMedia.setCourseId(courseBase.getId());
        teachplanMedia.setMediaFilename(mediaVo.getFilename());
        teachplanMedia.setCreateDate(LocalDateTime.now());
        teachplanMedia.setCreatePeople(AuthInfoHolder.getUserId().toString());
        //5.保存数据库
        this.save(teachplanMedia);
        //6.返回od
        return ResponseResult.okResult();
    }
}
